package cn.ven.dao;

import cn.ven.entity.Support;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class SupportDaoImpl implements SupportDao {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void add(Support support) {
        mongoTemplate.insert(support);
    }

    @Override
    public List<Support> findSupport(String uid) {
        Query query =new Query(Criteria.where("uid").is(uid));
        return mongoTemplate.find(query,Support.class);
    }

    @Override
    public Support findOneSupport(String uid, String blogid) {
        Query query =new Query();
        query.addCriteria(Criteria.where("uid").is(uid).and("blogid").is(blogid));
        return mongoTemplate.findOne(query,Support.class);
  }

    @Override
    public void delSupport(String uid, String blogid) {
        Query query =new Query();
        query.addCriteria(Criteria.where("uid").is(uid).and("blogid").is(blogid));
        mongoTemplate.remove(query,Support.class);
    }

    @Override
    public List<Support> findSupportByBlogid(String blogid) {

        Query query =new Query(Criteria.where("blogid").is(blogid));
        return mongoTemplate.find(query,Support.class);
    }

    @Override
    public void delSupportByBlogid(String blogid) {
        Query query =new Query();
        query.addCriteria(Criteria.where("blogid").is(blogid));
        mongoTemplate.remove(query,Support.class);
    }
}
